#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import argparse
import logging
import telebot

log = telebot.logger

def Args():

    parser = argparse.ArgumentParser()

    parser.add_argument(
          '-c', '--config'
        , dest="config_filename"
        , default=os.path.dirname(__file__) + "/config.json"
    )
    parser.add_argument(
          '-l', '--logfile'
        , dest="log_filename"
        , default='barin.log'
        , help="Logging to file <log_filename>"
            )
    parser.add_argument(
          "-v", "--verbose"
        , action="store_true"
        , dest="verbose"
        , default=False
        , help="Verbose prints enabled"
    )

    return parser.parse_args()

#------------------------------------------------------------------------------
def setLoggerImpl(
      log_filename
    , verbose
):
    global log

    #out_hdlr = None
    #if log_filename is not None:
    #    out_hdlr = logging.FileHandler(log_filename)
    #else:
    #    out_hdlr = logging.StreamHandler(sys.stdout)
    #
    #out_hdlr.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s]: %(message)s'))

    #log.addHandler(out_hdlr)
    log.setLevel(logging.DEBUG if verbose is True else logging.INFO)

def setLogger(args):
    setLoggerImpl(
          args.log_filename
        , args.verbose
    )

#------------------------------------------------------------------------------
def errStr(err, msg=''):

    exc_type, exc_value, exc_traceback = sys.exc_info()
    ret = 'ERROR:'
    ret += str(err)
    ret += ' (' + type(err).__name__ + ') '
    ret += exc_traceback.tb_frame.f_code.co_filename  + ':'
    ret += str(err.__traceback__.tb_lineno)
    ret += ' ' + msg
    return ret

#------------------------------------------------------------------------------
def printErrEx(
      err
    , msg = ''
):
    global log
    log.error(errStr(err,msg))

#------------------------------------------------------------------------------
def printErrInDebug(
      err
    , msg = ''
):
    global log
    log.debug(errStr(err,msg))

